package com.eqdd.yiqidian.ui.discover.college;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.CollegeLectureInfoBean;
import com.eqdd.yiqidian.box.ItemDecoration.SectionDividerItemDecoration;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.EditTextActivity;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.willy.ratingbar.ScaleRatingBar;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;

/**
 * @author吕志豪 .
 * @date 18-3-14  上午10:04.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class EvaluateActivity extends BaseActivity {

    private RecyclerViewCustom dataBinding;
    private int id;
    private SlimAdapterEx slimAdapterEx;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "对讲师的评价");

    }

    @Override
    public void initData() {

        id = getIntent().getIntExtra(Config.ID, 0);
        ArrayList<Object> scores = new ArrayList<>();
        scores.add(new Score("1.紧扣培训主题", 0));
        scores.add(new Score("2.内容充实、逻辑清楚", 0));
        scores.add(new Score("3.课程准备充足，概念清晰,内容丰富,表述形式多样", 0));
        scores.add(new Score("4.能够针对参训人员特点安排课堂活动，互动性强", 0));
        scores.add(new Score("5.培训内容与你的期望相符合", 0));
        scores.add(new Score("6.提供有实际意义的案例，对工作的启发性较大", 0));
        scores.add(new Score("7.能对参训人员的疑问作出专业的解答", 0));
        scores.add(new Score("8.获得适用的新知识，而且可以应用到工作中", 0));
        scores.add(new Score("9.拓宽了知识面或新的思路", 0));
        scores.add(new Score("10.值得我来听，并且愿意向其他同事推荐", 0));
        scores.add("11.您在本次培训中的收获与体会");
        scores.add("12.您对后续组织此类培训的意见与建议");
        scores.add(1);
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.recyclerView.addItemDecoration(new SectionDividerItemDecoration(this, 1, 2, 2, 6, 3)
                .setTitle("课程详情", "老师对课程的设计",
                        "培训老师",
                        "培训效果",
                        "其它"));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_116, new SlimInjector<Score>() {
            @Override
            public void onInject(@Nullable Score data, @Nullable IViewInjector injector) {
                injector.text(R.id.tv_one, data.getTitle())
                        .with(R.id.rb_score, view -> {
                            ScaleRatingBar ratingBar = (ScaleRatingBar) view;
                            ratingBar.setRating(data.getScore());
                            ratingBar.setOnRatingChangeListener((baseRatingBar, v) -> {
                                data.setScore(v);
                            });
                        });
            }
        }).register(R.layout.list_item_01, new SlimInjector<String>() {
            @Override
            public void onInject(@Nullable String data, @Nullable IViewInjector injector) {
                injector.text(R.id.tv_name, data);
            }
        })
                .register(R.layout.list_item_117, new SlimInjector<Integer>() {
                    @Override
                    public void onInject(@Nullable Integer data, @Nullable IViewInjector injector) {
                        injector.clicked(R.id.bt_submit, v -> {

                        });
                    }
                }).register(R.layout.list_item_118, new SlimInjector<CollegeLectureInfoBean>() {
                    @Override
                    public void onInject(@Nullable CollegeLectureInfoBean data, @Nullable IViewInjector injector) {
                        injector.text(R.id.tv_title, data.getTheTheme())
                                .with(R.id.tv_header, view -> {
                                    TextView textView = (TextView) view;
                                    StringBuilder append = new StringBuilder("主办部门:")
                                            .append(data.getHostdepName())
                                            .append("\n")
                                            .append("培训负责人:")
                                            .append(data.getRespoPersonName())
                                            .append("\n")
                                            .append("培训老师:")
                                            .append(data.getTeacherName())
                                            .append("\n")
                                            .append("培训时间:");
                                    Flowable.fromIterable(data.getTrainTimes())
                                            .subscribe(s -> append.append("\n").append("\r\r").append(s));
                                    textView.setText(append);
                                })
                                .text(R.id.tv_content, "亲爱的学员：\n    感谢您参与并完成本次培训！为进一步提高后期的培训质量，请您根据此次培训填写并完成以内容。\n    您的建议是提升培训质量的源泉，再次感谢您的支持！");
                    }
                }).
                        attachTo(dataBinding.recyclerView).updateData(scores);
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    if (slimAdapterEx.getDataItem(position) instanceof String) {
                        Bundle bundle = new Bundle();
                        bundle.putString("content", "");
                        bundle.putString("title", "请填写");
                        bundle.putBoolean("canBlank", false);
                        startActivityForResult(EditTextActivity.class, bundle, position);
                    }
                });
    }

    @Override
    public void setView() {

        OkGo.<HttpResult<List<CollegeLectureInfoBean>>>post(HttpConfig.BASE_URL + HttpConfig.COLLEGE_EVALUATE)
                .params("userGuid", AppConstent.user.getGuid())
                .params("courseId", id)
                .execute(new DialogCallBack<HttpResult<List<CollegeLectureInfoBean>>>(EvaluateActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<CollegeLectureInfoBean>>> response) {
                        HttpResult<List<CollegeLectureInfoBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200 && httpResult.getItems().size() > 0) {
                            slimAdapterEx.getData().add(0, httpResult.getItems().get(0));
                            slimAdapterEx.notifyItemInserted(0);
                            dataBinding.recyclerView.scrollToPosition(0);
                        }
                    }
                });
    }

    public class Score {
        String title;
        float score;

        public Score(String title, float score) {
            this.title = title;
            this.score = score;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public float getScore() {
            return score;
        }

        public void setScore(float score) {
            this.score = score;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.INTENT_CONTENT) {
            String content = data.getStringExtra("content");
            slimAdapterEx.getData().set(requestCode, content);
            slimAdapterEx.notifyItemChanged(requestCode);
        }
    }
}
